//
//  DHSaleViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/5/6.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSaleViewController.h"
#import "DHSale_DiscountView.h"
#import "DHSale_LimitedView.h"
#import "DHSale_GoodShopView.h"
#import "DHSale_SelfView.h"
#import "DHSale_FilterView.h"

@interface DHSaleViewController ()

@property (weak, nonatomic) IBOutlet UIView *tabView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tabBGViewLeftLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tabBGViewWidthLayout;
@property (weak, nonatomic) IBOutlet UIStackView *tabStackView;
@property (weak, nonatomic) IBOutlet UIView *tabBGView;
@property (weak, nonatomic) IBOutlet UIButton *filterButton;

@property (strong, nonatomic) DHSale_DiscountView *discountView; //折扣
@property (strong, nonatomic) DHSale_LimitedView *limitedView; //限量
@property (strong, nonatomic) DHSale_GoodShopView *goodShopView; //好店
@property (strong, nonatomic) DHSale_SelfView *selfView; //自营
@property (strong, nonatomic) DHSale_FilterView *filterView; //筛选

@property (strong, nonatomic) UIButton *indexButton;

@property (assign, nonatomic) BOOL isFilter;

@end

@implementation DHSaleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView setTitleWithString:@"折扣"];
    
    self.tabBGViewWidthLayout.constant = (kScreenWidth - 40) / 4 - 30;
    self.tabBGViewLeftLayout.constant = 15;
    
    CGFloat space = 0;
    if (iPhoneX) {
        space = 24;
    }
    self.discountView = [DHSale_DiscountView shareDHSale_DiscountView];
    self.discountView.frame = CGRectMake(0, self.tabView.bottom + space, self.view.width, self.view.height - self.navigationView.height - 38 - self.tabBarController.tabBar.height);
    [self.view addSubview:self.discountView];
    
    self.limitedView = [DHSale_LimitedView shareDHSale_LimitedView];
    self.limitedView.frame = CGRectMake(0, self.tabView.bottom + space, self.view.width, self.view.height - self.navigationView.height - 38 - self.tabBarController.tabBar.height);
    [self.view addSubview:self.limitedView];
    
    self.goodShopView = [DHSale_GoodShopView shareDHSale_GoodShopView];
    self.goodShopView.frame = CGRectMake(0, self.tabView.bottom + space, self.view.width, self.view.height - self.navigationView.height - 38 - self.tabBarController.tabBar.height);
    [self.view addSubview:self.goodShopView];
    
    self.selfView = [DHSale_SelfView shareDHSale_SelfView];
    self.selfView.frame = CGRectMake(0, self.tabView.bottom + space, self.view.width, self.view.height - self.navigationView.height - 38 - self.tabBarController.tabBar.height);
    [self.view addSubview:self.selfView];
    
    self.filterView = [DHSale_FilterView shareDHSale_FilterView];
    self.filterView.frame = CGRectMake(0, self.tabView.bottom + space, self.view.width, 1000);
    [self.view addSubview:self.filterView];
    
    self.discountView.hidden = NO;
    self.limitedView.hidden = YES;
    self.goodShopView.hidden = YES;
    self.selfView.hidden = YES;
    self.filterView.hidden = YES;
    
    [self.discountView loadDiscountViewData];
}

- (IBAction)tabAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (btn.tag != 1004) {
        self.indexButton = btn;
        for (UIButton *btn in self.tabStackView.subviews) {
            [btn setTitleColor:[UIColor colorWithHexString:@"#575757"] forState:UIControlStateNormal];
        }
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    switch (btn.tag) {
        case 1000:{
            [self.navigationView setTitleWithString:@"折扣"];
            self.tabBGViewLeftLayout.constant = btn.left + 15;
            self.discountView.hidden = NO;
            self.limitedView.hidden = YES;
            self.goodShopView.hidden = YES;
            self.selfView.hidden = YES;
            [self.discountView loadDiscountViewData];
            break;
        }
        case 1001:{
            [self.navigationView setTitleWithString:@"限量"];
            self.tabBGViewLeftLayout.constant = btn.left + 15;
            self.discountView.hidden = YES;
            self.limitedView.hidden = NO;
            self.goodShopView.hidden = YES;
            self.selfView.hidden = YES;
            [self.limitedView loadLimitedViewData];
            break;
        }
        case 1002:{
            [self.navigationView setTitleWithString:@"好店"];
            self.tabBGViewLeftLayout.constant = btn.left + 15;
            self.discountView.hidden = YES;
            self.limitedView.hidden = YES;
            self.goodShopView.hidden = NO;
            self.selfView.hidden = YES;
            [self.goodShopView loadGoodShopViewData];
            break;
        }
        case 1003:{
            [self.navigationView setTitleWithString:@"自营"];
            self.tabBGViewLeftLayout.constant = btn.left + 15;
            self.discountView.hidden = YES;
            self.limitedView.hidden = YES;
            self.goodShopView.hidden = YES;
            self.selfView.hidden = NO;
            [self.selfView loadDHSale_SelfViewData];
            break;
        }
        case 1004:{
            if (self.isFilter) {
                self.filterView.hidden = YES;
                [self.filterButton setImage:[UIImage imageNamed:@"sale_filter"] forState:UIControlStateNormal];
                self.isFilter = NO;
            }else{
                self.filterView.hidden = NO;
                [self.filterButton setImage:[UIImage imageNamed:@"sale_filtered"] forState:UIControlStateNormal];
                self.isFilter = YES;
                [self.filterView loadDHSale_FilterViewData];
            }
            break;
        }
            
        default:
            break;
    }
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"hide_filter_view"]) {
        self.filterView.hidden = YES;
        [self.filterButton setImage:[UIImage imageNamed:@"sale_filter"] forState:UIControlStateNormal];
        self.isFilter = NO;
    }else if ([eventName isEqualToString:@"filter_update"]) {
        if (!self.indexButton) {
            [self.discountView loadDiscountViewData];
        }
        switch (self.indexButton.tag) {
            case 1000:{
                [self.discountView loadDiscountViewData];
                break;
            }
            case 1001:{
                [self.limitedView loadLimitedViewData];
                break;
            }
            case 1002:{
                [self.goodShopView loadGoodShopViewData];
                break;
            }
            case 1003:{
                [self.selfView loadDHSale_SelfViewData];
                break;
            }
                
            default:
                break;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
